package matthias.WagoApp;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class settings_project extends Activity{
	
	public String ip; 
	public Boolean en_sim;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		try {
			setContentView(R.layout.settings_layout);
			final Button btnSave = (Button) findViewById(R.id.btnSave);
			btnSave.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					saveSettings();
					if (en_sim){
						Toast.makeText(getBaseContext(),getString(R.string.app_settings_saved),Toast.LENGTH_LONG).show();
					}else{
				        Toast.makeText(getBaseContext(), getString(R.string.app_settings_active_after_restart), Toast.LENGTH_LONG).show();
					}
					settings_project .this.finish();
					finish();
				}
			});
			
			final Button btnCancel = (Button) findViewById(R.id.btnCAncel);
			btnCancel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					settings_project .this.finish();					
				}
			});
			
			
			try{
				//get value passed from main activity when start this
				Intent i =  getIntent();
				
				ip = i.getStringExtra("ip");
				EditText edtxtIp = (EditText) findViewById(R.id.edtxtIp);
				edtxtIp.setText(ip);
				
				String sss =i.getStringExtra("en_sim");
				//Toast.makeText(getBaseContext(),">sim= "+sss,Toast.LENGTH_SHORT).show();
	    		if (sss!= null){
	    			if(sss.compareTo("true")==0){
	    				en_sim=true;
			    	}else{
			    		en_sim=false;
			    	}
			    }else
		    		Toast.makeText(getBaseContext(),"en_sim=null!",Toast.LENGTH_SHORT).show();
	    			    		
			} catch (Exception e) {
				Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();				
			}
				
			// assign every value to its object in this page
			CheckBox ckbEnSim =  (CheckBox) findViewById(R.id.checkBox1);
			ckbEnSim.setText(getString(R.string.app_check_box1));
			
			if (en_sim==true){
				ckbEnSim.setChecked(true);
			}else{
				ckbEnSim.setChecked(false);
			}
			
			ckbEnSim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					en_sim=isChecked;
		    	}
			});
			
			
			
		} catch (Exception e) {
			Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
		}
	}
	
	protected void onStop(){
        super.onStop();      
    }
	
	public void saveSettings(){
		try {
			// send back settings to main activity
			Intent i = getIntent();
			EditText edtxtIp = (EditText) findViewById(R.id.edtxtIp);
			CheckBox ckbEnSim =  (CheckBox) findViewById(R.id.checkBox1);
			ip = edtxtIp.getText().toString();
			en_sim=ckbEnSim.isChecked();
    		i.putExtra("ip", ip);
			i.putExtra("en_sim", ""+ckbEnSim.isChecked());
			setResult(RESULT_OK, i);
    		
    		// save settings
			SharedPreferences settings = getSharedPreferences("PREFS_WAGOAPP", 0);
    		SharedPreferences.Editor editor = settings.edit();
			editor.putString("ip", ip);
			editor.putBoolean("en_sim", en_sim);
			editor.commit();
    		
    	} catch (Exception e) {
			Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();				
		}		
	}
}